<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/6
 * Time: 17:19
 */
namespace app\index\controller;
//项目管理

use app\common\model\ProjectModel;
use app\index\model\UserModel;
use mg_api\Mg_api;

class Status extends Common{
    public function index(){

        //获取全部项目(本地)

        $all = ProjectModel::where('user_id',$_SESSION['user_id'])->select();

       // $all = $this->get_project('全部');
      //  $this->assign('all',$all);

        //已报备
        $baobei = $this->get_project('已报备');
	
    //    $this->assign('baobei',$baobei);
        //已签单
        $sign = $this->get_project('已签单');
		$data=[
			'all'=>$all,
			'baobei'=>$baobei,
			'sign'=>$sign
		];

		//return json(['code'=>0,'msg'=>"ok",'data'=>$sign]);

       // $this->assign('data',json($sign));
        return view('index');
    }
	public function index_ajax(){
		  //获取全部项目(本地)

        $all = ProjectModel::where('user_id',$_SESSION['user_id'])->select();
       // $all = $this->get_project('全部');
      //  $this->assign('all',$all);

        //已报备
        //$baobei = $this->get_project('已报备');
		$baobei = ProjectModel::where('user_id',$_SESSION['user_id'])->where('prostatus','已报备')->select();
		
	
    //    $this->assign('baobei',$baobei);
        //已签单
     //   $sign = $this->get_project('已签单');
		$sign = ProjectModel::where('user_id',$_SESSION['user_id'])->where('prostatus','已签单')->select();
		$data=[
			'all'=>$all,
			'baobei'=>$baobei,
			'sign'=>$sign
		];

		return json(['code'=>0,'msg'=>"ok",'data'=>$data]);
	}
    //报备
    public function baobei(){
        //获取报备项目
        if($_SESSION['openid']){
            $openid = $_SESSION['openid'];
        }else if($_SESSION['user_id']){
           $user =  UserModel::get($_SESSION['user_id']);
            $openid = $user->openid;
        }
        //获取报备项目
        $mg_api = new Mg_api();
        $data= [
            'key'=>'Get_Project_List',
            'wx_openid'=>$openid,
            'types'=>'已报备'
        ];

        $re = $mg_api->api($data);
        $re = json_decode($re,true);
        $this->assign('project',$re['JData']);
        return view('baobei');
    }

    //获取项目信息
    private function get_project($type){
        $mg_api = new Mg_api();
        if(empty($_SESSION['openid'])){
            $this->error('没有获取到openid');
        }
        $data = [
            'key'=>'Get_Project_List',
            'wx_openid'=>$_SESSION['openid'],
            'types'=>$type

        ];
        $re = $mg_api->api($data);
        $re = json_decode($re,true);
        if($re['Status']){
            return $re['JData'];
        }else{
            return false;
        }
    }
	//获取获取项目 的状态
	public function xiangqing($address){
	//	$address = mb_convert_encoding($address,"utf-8", "gbk");
		//查询该项目的所有状态;
		
		$baobei = ProjectModel::where('address',$address)->where('prostatus','已报备')->find();
		$qiandan = ProjectModel::where('address',$address)->where('prostatus','已签单')->find();
		$wangong = ProjectModel::where('address',$address)->where('prostatus','已完工')->find();
		$fafang = ProjectModel::where('address',$address)->where('linkstatus','已发放')->find();
		$data = [
			'baobei'=>$baobei,
			'qiandan'=>$qiandan,
			'wangong'=>$wangong,
			'fafang'=>$fafang
		];
		$this->assign('data',$data);
		$address = substr($address,0,strlen($address)-4).'*号';
		$this->assign('address',$address);
		
		return view('details');
		
	}

	




}